package com.dongdong.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.dongdong.service.SeckillService;
import com.dongdong.utils.Result;
import com.dongdong.utils.ResultEnum;
import com.dongdong.vo.SeckillGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/seckill")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    @GetMapping("/redisHot")
    public Result redisHot(){
        seckillService.redisHot();
        return Result.OK();
    }

    @GetMapping("/getSeckillList")
    @SentinelResource(value = "getSeckillList", blockHandler= "skillGoods")
    public Result getSeckillList(){
        List<SeckillGoodsVO> list = seckillService.getSeckillList();

        return Result.OK().setData(list);
    }

    //降级方法
    public Result skillGoods(BlockException e){
        return Result.DATA(ResultEnum.SKILL_MORE);
    }


    @GetMapping("/getSecKillById")
    public Result getSecKillById(@RequestParam Long id){
        SeckillGoodsVO seckillGoodsVO = seckillService.getSecKillById(id);
        return Result.OK().setData(seckillGoodsVO);
    }

    @PostMapping("/updateSeckillGoodsVO")
    public void updateSeckillGoodsVO(@RequestBody SeckillGoodsVO seckillGoodsVO){
        seckillService.updateSeckillGoodsVO(seckillGoodsVO);
    }














    @GetMapping("/query")
    public String queryOrder() {
        // 查询商品
        seckillService.queryGoods();
        // 查询订单
        System.out.println("查询订单");
        return "查询订单成功";
    }

    @GetMapping("/save")
    public String saveOrder() {
        // 查询商品
        seckillService.queryGoods();
        // 查询订单
        System.err.println("新增订单");
        return "新增订单成功";
    }
    @GetMapping("/update")
    public String updateOrder() {
        return "更新订单成功";
    }

    @GetMapping("getSecKill")
    public Result getSecKill(@RequestParam Long id){
        SeckillGoodsVO seckillGoodsVO = seckillService.getSecKill(id);
        return Result.OK().setData(seckillGoodsVO);
    }
}
